<template>
  <el-dialog
    :title="dialist.isAdd ? '角色添加' : '角色编辑'"
    :visible.sync="dialist.isShow"
    center
    :before-close="cancle"
  >
    <el-form :model="list" ref="diaform" :rules="rules">
      <el-form-item
        label="角色名称"
        :label-width="formLabelWidth"
        prop="rolename"
      >
        <el-input v-model="list.rolename" autocomplete="off"></el-input>
      </el-form-item>
    <!-- 
        :data="roleOption" 绑定的数据 也是要显示的值
        show-checkbox 可以选 复选框
        node-key="id" 绑定的属性，后面 getcheckkey里面的值
         :props="defaultProps" 要展示的属性，对象
         default-expanded-keys  默认展开选中的树 值是数组
         check-strictly 父子不关联
     -->
      <el-form-item label="角色权限" :label-width="formLabelWidth">
        <el-tree 
          :data="roleOption"
          show-checkbox
          node-key="id"
          ref="roletree"
          highlight-current
          :props="defaultProps"
          check-strictly
          default-expand-all
          
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="list.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" v-if="dialist.isAdd" @click="add"
        >添 加</el-button
      >
      <el-button type="primary" v-else @click="edit">编 辑</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, getRoleOne, editRole, getRole,getMenu } from "../../util/axios";
export default {
  data() {
    return {
      list: {
        rolename: "",
        status: 1,
        menus: [],
      },
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "title",
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
        roleOption: [],
        //点击编辑后展开选中的数组
        newArr:[],
    };
  },
  props: ["dialist"],
  methods: {
    //点击取消后隐藏dialog
    cancle() {
      //隐藏dialog
      this.$emit("cancle", false);
      //清空表格
      this.list = {
        // id: 0,
        rolename: "",
        status: 1,
        menus: [],
      };
      //清空验证
      this.$refs.diaform.resetFields();
      
      //清空选中权限
    //   this.newArr = null;
      this.$refs.roletree.setCheckedKeys([])
      
    },
    //添加功能
    add() {
      this.$refs.diaform.validate((val) => {
        if (val) {
           this.list.menus=this.$refs.roletree.getCheckedKeys()?JSON.stringify(this.$refs.roletree.getCheckedKeys()):"";
          addRole(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.$emit("adds");
                this.cancle()
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    //dialog的编辑
    edit() {
      this.$refs.diaform.validate((val) => {
        if (val) {
           this.list.menus=this.$refs.roletree.getCheckedKeys()?JSON.stringify(this.$refs.roletree.getCheckedKeys()):"";
          editRole(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.$emit("adds");
                this.cancle()
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    lookup(id) {
      getRoleOne({ id }).then((res) => {
        if (res.code == 200) {  
          this.list = res.list;
          this.list.id = id;
        //获取到接口的值是一个字符串，需要转数值类型的数组
          let  newArr = JSON.parse(res.list.menus).map(val=>{
                return Number(val)
            });
            // console.log(this.newArr);
            //数组回显到角色权限内部。参数必须是数字类型的数组
          this.$refs.roletree.setCheckedKeys(newArr)
        }
      });
    },
  },
  mounted() {
    //页面挂载后循环数据
    getMenu({istree:true}).then((res) => {
      if (res.code == 200) {
        this.roleOption = res.list;
      } else {
        this.$message.error(res.msg);
      }
    });
  },
};
</script>

<style lang="stylus" scoped></style>